package zwp.exercise.service.exercisedemo.casedemo.operator;

/**
 * @Author zwp14
 * @create 2025/10/22 21:59
 * @description 运算符练习
 */
public class OperatorDemo {
    public static void main(String[] args) {
        // 计算机车加速度
        computerCarAcceleration();

        // 求解二元一次方程组
        solvingASystemOfBinaryLinearEquations();
    }

    /**
     * 使用克莱姆法则求解二元一次方程组
     * {
     *     21.8x + 2y = 28
     *     7x + 8y = 62
     * }
     * x = (ed - bf) / (ad - bc)
     * y = (af - ec) / (ad - bc)
     */
    private static void solvingASystemOfBinaryLinearEquations() {
        double a = 21.8;
        int b = 2;
        int c = 7;
        int d = 8;
        int e = 28;
        int f = 62;

        double denominator = (a * d) - (b * c);
        System.out.println("共同的分母：" + denominator);
        // 求x和y
        double x = ((e * d) - (b * f)) / denominator;
        double y = ((a * f) - (e * c)) / denominator;

        System.out.println("求出x的值：" + x);
        System.out.println("求出y的值：" + y);
    }

    /**
     * 一辆车，用了8.7秒从0加速到100,计算车的平均加速度
     * 速度变化量除以时间为平价加速度
     */
    private static void computerCarAcceleration() {
        double startTime = 0;
        double endTime = 8.7;
        double costTime = endTime - startTime;

        int startSpeed = 0;
        int endSpeed = 100;
        int difSpeed = endSpeed - startSpeed;

        // 计算平均加速度
        double averageAcceleration = difSpeed / costTime;
        System.out.println("从0加速到100所花费的时间：" + costTime);
        System.out.println("速度变化量：" + difSpeed);
        System.out.println("平均加速度：" + averageAcceleration);

        // 对平均加速度四舍五入，保留两位小数
        System.out.println("对平均加速度四舍五入：" +  String.format("%.2f",averageAcceleration));
    }
}
